package com.sk.bilibili.model.fragment.member;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.bean.Dynamic;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.dto.Page;
import com.sk.bilibili.mapper.DynamicMapper;
import com.sk.bilibili.mapper.PraiseMapper;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.model.BaseRefreshModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.view.fragment.member.DynamicFragment;
import com.sk.bilibili.view.fragment.member.HomeFragment;

public class DynamicModel extends BaseRefreshModel<DynamicFragment> {

    @Autowired
    DynamicMapper dynamicMapper;
    @Autowired
    PraiseMapper praiseMapper;
    private int page = 1;
    private int size = 10;

    public void refreshDynamic(Integer memberId) {
        page = 1;
        JsonMap map = getJsonMap(memberId);
        RequestUtils.process(dynamicMapper.getMemberDynamic(map), dynamicPage -> {
            view.refreshSuccess(dynamicPage.getRecords());
            page++;
        });
    }

    private JsonMap getJsonMap(Integer memberId) {
        JsonMap map = new JsonMap();
        map.put("page", page);
        map.put("size", size);
        map.put("memberId", memberId);
        return map;
    }

    public void loadMore(Integer memberId) {
        JsonMap map = getJsonMap(memberId);
        RequestUtils.process(dynamicMapper.getMemberDynamic(map), dynamicPage -> {
            view.loadSuccess(dynamicPage.getRecords());
            page++;
        });
    }

    public void addPraise(int type, Integer targetId) {
        JsonMap map = new JsonMap();
        map.put("type",type);
        map.put("targetId",targetId);
        RequestUtils.process(praiseMapper.switchPraise(map),
                aBoolean -> view.praiseSuccess(aBoolean));
    }

}